package matt.design;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class UsersDataSource {

	private SQLiteDatabase database;
	private UserSQLiteHelper dbHelper;
	@SuppressWarnings("unused")
	private String[] allColumns = {UserSQLiteHelper.COLUMN_ID, UserSQLiteHelper.COLUMN_FIRST,
			UserSQLiteHelper.COLUMN_LAST, UserSQLiteHelper.COLUMN_PASSWORD};
		
	/**
	 * takes in the context and gets an instance of the database helper
	 * @param context
	 */
	public UsersDataSource(Context context) {
		dbHelper = new UserSQLiteHelper(context);
	}
	
	/**
	 * gets a Usable database
	 * @throws SQLException
	 */
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	/**
	 * closes the database
	 */
	public void close() {
		dbHelper.close();
	}
	
	/**
	 * puts a new user's info into the database
	 * @param email
	 * @param first
	 * @param last
	 * @param password
	 */
	public void createUser(String email, String first, String last, String password) {
		ContentValues values = new ContentValues();
		values.put(UserSQLiteHelper.COLUMN_ID, email);
		values.put(UserSQLiteHelper.COLUMN_FIRST, first);
		values.put(UserSQLiteHelper.COLUMN_LAST, last);
		values.put(UserSQLiteHelper.COLUMN_PASSWORD, password);
		database.insert(UserSQLiteHelper.TABLE_USERS, null, values);
	}
	
	/**
	 * Queries the database to see if a user exists
	 * @param email
	 * @return
	 */
	public boolean containsUser(String email) {
		Cursor cursor = dbHelper.getReadableDatabase().
				rawQuery("SELECT *"
						+ " FROM " + UserSQLiteHelper.TABLE_USERS
						+ " WHERE " + UserSQLiteHelper.COLUMN_ID + " = ?", new String[] { email });
		return cursor.getCount() != 0;
	}
	
	/**
	 * Check the password for a valid login
	 * @param email
	 * @param passwordAttempt
	 * @return
	 */
	public boolean checkPassword(String email, String passwordAttempt) {
		Cursor cursor = dbHelper.getReadableDatabase().
				rawQuery("SELECT " + UserSQLiteHelper.COLUMN_PASSWORD
						+ " FROM " + UserSQLiteHelper.TABLE_USERS
						+ " WHERE " + UserSQLiteHelper.COLUMN_ID + " = ? " 
						+ " AND " + UserSQLiteHelper.COLUMN_PASSWORD + " = ?", new String[] { email, passwordAttempt });
		return cursor.getCount() != 0;
	}
	
	public boolean changePassword(String email, String oldPassword, String newPassword, String checkNewPassword){
		if(checkPassword(email, oldPassword) && newPassword.equals(checkNewPassword)){
			Cursor cursor = dbHelper.getReadableDatabase().
					rawQuery("SELECT " + UserSQLiteHelper.COLUMN_PASSWORD
							+ " FROM " + UserSQLiteHelper.TABLE_USERS
							+ " WHERE " + UserSQLiteHelper.COLUMN_ID + " = ? " 
							+ " AND " + UserSQLiteHelper.COLUMN_PASSWORD + " = ?", new String[] { email, oldPassword });
			String first, last;
			cursor.moveToFirst();
			first = cursor.getString(1);
			last = cursor.getString(2);
			
			ContentValues values = new ContentValues();
			values.put(UserSQLiteHelper.COLUMN_ID, email);
			values.put(UserSQLiteHelper.COLUMN_FIRST, first);
			values.put(UserSQLiteHelper.COLUMN_LAST, last);
			values.put(UserSQLiteHelper.COLUMN_PASSWORD, newPassword);
			
			database.update(UserSQLiteHelper.TABLE_USERS, values,  
					UserSQLiteHelper.COLUMN_ID + " = ?", new String[] { email });
			
			return true;
		}
		return false;
	}
}